﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Sofire.Serialization;

namespace SofireCodeGenerator
{
    /// <summary>
    /// 全局配置。
    /// </summary>
    public class GlobalSetting
    {
        static GlobalSetting()
        {
            if(File.Exists(GlobalSettingPath))
            {
                var r = Serializer.Read<GlobalSetting>(GlobalSettingPath);
                if(r.IsSucceed) _Current = r.Value;
            }
            else
            {
                _Current = new GlobalSetting();
            }
        }

        private readonly static FastBinarySerializer Serializer = new FastBinarySerializer();
        private readonly static string GlobalSettingPath = Path.Combine(GA.AppDirectory, "Global.Setting");
        private readonly static GlobalSetting _Current;

        private GlobalSetting() { }

        private bool _AutoAliasName = true;
        /// <summary>
        /// 设置或获取一个值，指示是否自动识别名称。默认为 true。
        /// </summary>
        public static bool AutoAliasName
        {
            get
            {
                return _Current._AutoAliasName;
            }
            set
            {
                _Current._AutoAliasName = value;
            }
        }

        private bool _AlwaysNullable = false;
        /// <summary>
        /// 设置或获取一个值，指示无论数据库字段类型是否允许为空，都采用可空类型。默认为 false。
        /// </summary>
        public static bool AlwaysNullable
        {
            get
            {
                return _Current._AlwaysNullable;
            }
            set
            {
                _Current._AlwaysNullable = value;
            }
        }

        private string _EntitySuffix = "Entity";
        /// <summary>
        /// 设置或获取实体的默认后缀名。
        /// </summary>
        public static string EntitySuffix
        {
            get
            {
                return _Current._EntitySuffix;
            }
            set
            {
                _Current._EntitySuffix = value;
            }
        }

        private string _TemplatesDirectory = GA.AppDirectory + "Templates\\Default";
        /// <summary>
        /// 设置或获取模板的目录。
        /// </summary>
        public static string TemplatesDirectory
        {
            get
            {
                return _Current._TemplatesDirectory;
            }
            set
            {
                _Current._TemplatesDirectory = value;
            }
        }

        private string _XSqlDirectory = "XSql";
        /// <summary>
        /// 设置或获取默认 XSql 的目录。
        /// </summary>
        public static string XSqlDirectory
        {
            get
            {
                return _Current._XSqlDirectory;
            }
            set
            {
                _Current._XSqlDirectory = value;
            }
        }

        private bool _EnabledXSql = true;
        /// <summary>
        /// 设置或获取一个值，指示是是否启用 XSql。默认为 true。
        /// </summary>
        public static bool EnabledXSql
        {
            get
            {
                return _Current._EnabledXSql;
            }
            set
            {
                _Current._EnabledXSql = value;
            }
        }

        public static void Save()
        {
            Serializer.Write(GlobalSettingPath, _Current);
        }
    }

    public class GlobalParameters
    {
        /// <summary>
        /// 设置或获取项目的输出根目录。
        /// </summary>
        public static string OutputDirectory;
    }
}
